<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :form="true"
    :destroy-on-close="true"
    :width="460"
    v-model="visible"
    :title="isUpdate ? '修改就业统计-就业数据文件' : '添加就业统计-就业数据文件'"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.prevent=""
    >
      <el-form-item label="文件名" prop="fileName">
        <el-input
          clearable
          v-model="form.fileName"
          placeholder="请输入文件名"
        />
      </el-form-item>
      <el-form-item label="上传时间" prop="fileTime">
        <el-date-picker
          v-model="form.fileTime"
          value-format="YYYY-MM-DD"
          placeholder="请选择上传时间"
          class="ele-fluid"
        />
      </el-form-item>
      <el-form-item label="标识" prop="fileIndex">
        <el-input
          clearable
          v-model="form.fileIndex"
          placeholder="请输入标识"
        />
      </el-form-item>
      <el-form-item label="状态" prop="fileState">
        <el-input
          clearable
          v-model="form.fileState"
          placeholder="请输入状态"
        />
      </el-form-item>
      <el-form-item label="路径" prop="filePath">
        <el-input
          clearable
          v-model="form.filePath"
          placeholder="请输入路径"
        />
      </el-form-item>
      <el-form-item label="毕业年份" prop="fileYear">
        <el-input
          clearable
          v-model="form.fileYear"
          placeholder="请输入毕业年份"
        />
      </el-form-item>
      <el-form-item label="学生量" prop="studentCount">
        <el-input
          clearable
          v-model="form.studentCount"
          placeholder="请输入学生量"
        />
      </el-form-item>
      <el-form-item label="签约人数" prop="signingCount">
        <el-input
          clearable
          v-model="form.signingCount"
          placeholder="请输入签约人数"
        />
      </el-form-item>
      <el-form-item label="签约率" prop="signingRate">
        <el-input
          clearable
          v-model="form.signingRate"
          placeholder="请输入签约率"
        />
      </el-form-item>
      <el-form-item label="就业率" prop="employmentRate">
        <el-input
          clearable
          v-model="form.employmentRate"
          placeholder="请输入就业率"
        />
      </el-form-item>
      <el-form-item label="就业人数" prop="employmentCount">
        <el-input
          clearable
          v-model="form.employmentCount"
          placeholder="请输入就业人数"
        />
      </el-form-item>
      <el-form-item label="更新者" prop="updateBy">
        <el-input
          clearable
          v-model="form.updateBy"
          placeholder="请输入更新者"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker
          v-model="form.updateTime"
          value-format="YYYY-MM-DD"
          placeholder="请选择更新时间"
          class="ele-fluid"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import {ref, reactive, watch} from 'vue';
import {EleMessage} from 'ele-admin-plus';
import {useFormData} from '@/utils/use-form-data';
import {addFile, updateFile} from '@/api/manage/statistic/admin/fileInfo';

const props = defineProps({
  /** 修改回显的数据 */
  data: Object
});

const emit = defineEmits(['done']);

/** 弹窗是否打开 */
const visible = defineModel({type: Boolean});

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  id: void 0,
  fileName: '',
  fileTime: '',
  fileIndex: '',
  fileState: '',
  filePath: '',
  fileYear: '',
  studentCount: void 0,
  signingCount: void 0,
  signingRate: '',
  employmentRate: '',
  employmentCount: void 0,
  updateBy: '',
  updateTime: ''
});

/** 表单验证规则 */
const rules = reactive({
  fileYear: [
    {
      required: true,
      message: '请输入毕业年份',
      type: 'string',
      trigger: 'blur'
    }
  ]
});

/** 关闭弹窗 */
const handleCancel = () => {
  visible.value = false;
};

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateFile : addFile;
    saveOrUpdate(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success({message: msg, plain: true});
        handleCancel();
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error({message: e.message, plain: true});
      });
  });
};

/** 监听弹窗打开 */
watch(visible, () => {
  if (visible.value) {
    if (props.data) {
      assignFields(props.data);
      isUpdate.value = true;
    } else {
      resetFields();
      isUpdate.value = false;
    }
  }
});
</script>
